﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Niknak.Services.ServiceCore;

namespace Niknak.Services.TaskScheduler
{
    public class WcfEndpointHelper<ExceptionHandlerType> : Niknak.Services.ServiceCore.Wcf.Helper<WcfConfigurationSettings, Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient>
        where ExceptionHandlerType : IExceptionHandler, new()
    {
        public static ExceptionHandlerType CustomExceptionHandler
        {
            get { return new ExceptionHandlerType(); }
        }

        public static bool IsEndpointOnline()
        {
            return EndpointAvailable;
        }

        public static Guid[] GetRunningTaskIds()
        {
            Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient client = new Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient();
            try
            {
                if (EndpointAvailable)
                {
                    Guid[] value = client.GetRunningTaskIds();
                    client.Close();
                    return value;
                }
            }
            catch (Exception ex)
            {
                CustomExceptionHandler.ExceptionLogEntry(ex);
                ex.GetHashCode();
                if (client.State == System.ServiceModel.CommunicationState.Faulted)
                    client.Abort();
                else
                    client.Close();
            }

            return new Guid[0];
        }

        public static bool Reset()
        {
            Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient client = new Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient();
            try
            {
                if (EndpointAvailable)
                {
                    bool value = client.Reset();
                    client.Close();
                    return value;
                }
            }
            catch (Exception ex)
            {
                CustomExceptionHandler.ExceptionLogEntry(ex);
                ex.GetHashCode();
                if (client.State == System.ServiceModel.CommunicationState.Faulted)
                    client.Abort();
                else
                    client.Close();
            }

            return false;
        }

        public static bool Restart()
        {
            Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient client = new Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient();
            try
            {
                if (EndpointAvailable)
                {
                    bool value = client.Restart();
                    client.Close(); //unlikely we'll get this far
                }
            }
            catch { } //some kind of error is expected since we're yanking the plug

            return true; //since the application exits before it gets a chance to respond, assume (!) it was all good
        }

        public static bool RaiseIterationCompletedEvent(Guid scheduledTaskId, DateTime iterationDate)
        {
            Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient client = new Niknak.Services.TaskScheduler.Wcf.RemoteTaskScheduler.WcfEndpointClient();
            try
            {
                if (EndpointAvailable)
                {
                    bool value = client.RaiseIterationCompletedEvent(scheduledTaskId, iterationDate);
                    client.Close();
                    return value;
                }
            }
            catch (Exception ex)
            {
                CustomExceptionHandler.ExceptionLogEntry(ex);
                ex.GetHashCode();
                if (client.State == System.ServiceModel.CommunicationState.Faulted)
                    client.Abort();
                else
                    client.Close();
            }

            return false;
        }
    }
}
